const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../models/db");
const tool = require("../tools/tool");

// 自动离店设置
router.post('/sets/setAutoLeft', async ctx => {
    let id = ctx.request.body.id
    let autoLeft = ctx.request.body.autoLeft
    await db.models['admin'].update({
        autoLeft: autoLeft
    }, {
        where: {
            id: id
        }
    })
    return ctx.suc(autoLeft === 1 ? '已设置自动离店' : '已取消自动离店')
})

router.post('/sets/test', async ctx => {
    let list = await db.query(`select salesId,billDate,id,longitude,latitude  from contact_bf where tenantId=14 order by salesId asc,billDate, id asc  `, {type: QueryTypes.SELECT})
    for (let i = 1; i < list.length; i++) {
        let lastRow = list[i-1]
        let row = list[i]
        if(row.salesId === lastRow.salesId && row.billDate === lastRow.billDate) {
            if(row.longitude && row.latitude && lastRow.longitude && lastRow.latitude) {
                let distance = tool.getDistance(row.longitude, row.latitude, lastRow.longitude, lastRow.latitude)
                await db.models['contactBf'].update({
                        lastDistance: distance
                    },
                    {
                        where: {
                            id: row.id
                        }
                    })
            }
        }
    }
    ctx.suc('成功')
})



module.exports = router;